package com.ece.friendly.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ece.friendly.jdbcConnection.JdbcConnection;
import com.ece.friendly.model.Friend;
@Service
public class FriendDaoImp implements FriendDao{

	@Autowired
	private JdbcConnection jdbcConnection;
	
	@Override
	public Friend getFriend(String login, String password) {
		Friend friend=null;
		try {
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT * FROM Friend where login='"+login+"' and password='"+password+"' ;");
			while(resultat.next()){
				friend=new Friend(resultat.getString(1), resultat.getString(2), resultat.getString(3),resultat.getString(4),resultat.getString(5),resultat.getString(6),resultat.getString(7));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return friend;
	}

	@Override
	public boolean addFriend(String login, String password, String name,String firstname, String address, String city, String country) 
	{
		try {
			
			if(jdbcConnection.getStat().executeUpdate("insert into friend values ('"+login+"','"+password+"','"+name+"','"+firstname+"','"+address+"','"+city+"','"+country+"')")>0)
			{
				jdbcConnection.getConnection().commit();
				return true;
			}
			else return false;
		} catch (SQLException e1) {
			return false;
		}
	}

	@Override
	public Friend getFriend(String login) {
		Friend friend=null;
		try {
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT * FROM Friend where login='"+login+"' ;");
			while(resultat.next()){
				friend=new Friend(resultat.getString(1), resultat.getString(2), resultat.getString(3),resultat.getString(4),resultat.getString(5),resultat.getString(6),resultat.getString(7));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return friend;	}

}
